//
//  YZLoginVC.m
//  VET
//
//  Created by Lam BG on 2018/6/27.
//  Copyright © 2018年 YZ. All rights reserved.
//

// VC
#import "YZLoginVC.h"
#import "YZPhoneLoginVC.h"

// VM
#import "YZLoginVM.h"

// View
#import "YZStateBtn.h"
#import "YZGradientBtn.h"

// Other
#import "YZLauncher.h"
#import "YZStatementHeader.h"

// 第三方
#import "TBCityIconFont.h"

// Category
#import "SVProgressHUD+Message.h"
#import "UIColor+Category.h"

#import "YZForgetPWDVC.h"

@interface YZLoginVC () 

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *emptyHeightConst;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *emptyHeightConstSmall;


@property (weak, nonatomic) IBOutlet UITextField *numberTF;
@property (weak, nonatomic) IBOutlet UIImageView *numberInfoIV;
@property (weak, nonatomic) IBOutlet UIView *numberView;


@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet YZStateBtn *passwordStateBtn;
@property (weak, nonatomic) IBOutlet UIView *passwordView;


@property (weak, nonatomic) IBOutlet YZGradientBtn *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *weChatBtn;


@property (strong, nonatomic) YZLoginVM *loginVM;

@end

@implementation YZLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - private function

- (void)setup {
    [super setup];
    
    // vc title
    self.title = @"登录注册";
    
    // 左上角的按钮
//    UIBarButtonItem *popToPreButton = [self barButtonItemWithImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\ue600", 30, [UIColor blackColor])] highImage:nil target:self action:@selector(popToPre)];
//    self.navigationItem.leftBarButtonItem = popToPreButton;
    
    
    // numberInfoIV 显示信息图片
    self.numberInfoIV.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue608", 30, [UIColor redColor])];
    // 设置状态按钮图片
    [self.passwordStateBtn setStateImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\ue602", 30, [UIColor blackColor])] withSwitchImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\ue601", 30, [UIColor redColor])]];
    
    // 适配分辨率
    if (kIsiPhoneSmall || kIsiPhone6) {
        
        self.emptyHeightConst.constant = YZRealValue(40);
        if (kIsiPhoneSmall) {
            
            self.emptyHeightConstSmall.constant = YZRealValue(8);
        }
    }
}

- (void)bindVM {
    
    RAC(self.loginVM, number) = self.numberTF.rac_textSignal;
    RAC(self.loginVM, password) = self.passwordTF.rac_textSignal;
    
    // 账号
    @weakify(self)
//    [RACObserve(self.loginVM, isHideWarning) subscribeNext:^(NSString *hide) {
//
//        @strongify(self)
//        BOOL isHideWarning = [hide boolValue];
//        self.numberInfoIV.hidden = YES;
////        self.isHideWarning = isHideWarning;
//        if ([self.numberTF isFirstResponder]) {
//
//            if (isHideWarning) {
//
//                self.numberView.backgroundColor = yzGreenColor;
//            }
//
//            if (self.loginVM.numberWarningInfo.length > 0) {
//                if (isHideWarning) {
//
//                    self.numberView.backgroundColor = yzGreenColor;
//                    [SVProgressHUD yz_showMessage:self.loginVM.numberWarningInfo];
//                } else {
//
//                    self.numberView.backgroundColor = [UIColor redColor];
//                    [SVProgressHUD yz_showMessage:self.loginVM.numberWarningInfo];
//                }
//            }
//        }
//    }];
    
    // 密码明文切换
    [RACObserve(self.passwordStateBtn, isSwitch) subscribeNext:^(id  _Nullable switchFlag) {

        @strongify(self)
        BOOL isSwitch = [switchFlag boolValue];
        self.passwordTF.secureTextEntry = !isSwitch;

        NSString *text = self.passwordTF.text;
        self.passwordTF.text = @" ";
        self.passwordTF.text = text;

        if (self.passwordTF.secureTextEntry) {

            [self.passwordTF insertText:self.passwordTF.text];
        }
    }];
    
    // 登录按钮
    RAC(self.loginBtn, enabled) = self.loginVM.loginEnableSignal;
    [[self.loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        @strongify(self)
//        [self.loginVM.loginCmd execute:nil];
        [YZLauncher jumpToMain];

    }];
//    [[self.loginVM.loginCmd executionSignals] subscribeNext:^(RACSignal<id> *signal) {

//        [signal subscribeNext:^(id  _Nullable x) {
        
//            @strongify(self)
//            if ([YZLauncher isRegisted]) {
            
                // 登录成功流程
//                [YZLauncher jumpToMain];
//            } else {
//
//                // 需要完善部分信息
//                UIStoryboard *personalStoryboard = [UIStoryboard storyboardWithName:@"YZLaunchSB" bundle:nil];
//                UIViewController *vc = [personalStoryboard instantiateViewControllerWithIdentifier:@"YZAvatarVC"];
//                [self.navigationController pushViewController:vc animated:YES];
//            }
//        }];

//    }];
    [self.loginVM.loginCmd.errors subscribeNext:^(NSError * _Nullable x) {
        
        // 登录错误流程
        NSLog(@"subscribeNext: %@", x);
    }];
    
    
    // 微信登录
    self.weChatBtn.rac_command = self.loginVM.weChatCmd;
}

#pragma mark - event actions

- (void)popToPre {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)forgetPasswordClicked:(id)sender {
    
    // 跳转到找回密码页
    YZForgetPWDVC *forgetPWDVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZForgetPWDVC"];
    [self.navigationController pushViewController:forgetPWDVC animated:YES];
}

#pragma mark - getter 懒加载

- (YZLoginVM *)loginVM {
    
    if (_loginVM == nil) {
        
        _loginVM = [[YZLoginVM alloc] init];
    }
    
    return _loginVM;
}

@end
